package com.rub.android.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.rub.android.model.Tags
import com.rub.android.service.TagsService

class TagsViewModel(repository: TagsService) : ViewModel() {

    //请求参数
    private var requestData = MutableLiveData<String>()

    val responseData = Transformations.switchMap(requestData) {
        liveData {
            val result = try {
                val tag = repository.refreshTagLists()
                Result.success(tag)
            } catch (e: Exception) {
                Result.failure<Tags>(e)
            }
            emit(result)
        }
    }


    fun onRefresh() {
        requestData.value = requestData.value
    }
}